package cn.openbiotoken.android.facetoken;

import android.content.Context;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.nnf.nnfkitlib.NNF_FaceInfo;
import com.nnf.nnfkitlib.NNF_Image;

import java.util.Collections;
import java.util.List;

import cn.openbiotoken.android.UriBiometricsDetector;

public class HomoSapiensUriBiometricsDetector extends UriBiometricsDetector {

    public HomoSapiensUriBiometricsDetector(@NonNull final Context context, @NonNull final Uri source) {
        super(context, source);
    }

    @Nullable
    @Override
    public NNF_FaceInfo detect() {
        @NonNull final DetectionContext context = new DetectionContext.Builder().build();
        beforeInitializeDetectionContext(context);
        context.initialize();

        @NonNull final NNF_Image image = new NNF_Image();
        image.bitmap = uriBitmapHelper.loadBitmap();
        @Nullable final NNF_FaceInfo face = context.detect(image);

        context.release();

        return face;
    }

    @NonNull
    @Override
    public List<NNF_FaceInfo> detect(int limit) {
        @NonNull final DetectionContext context = new DetectionContext.Builder().build();
        beforeInitializeDetectionContext(context);
        context.initialize();

        @NonNull final NNF_Image image = new NNF_Image();
        image.bitmap = uriBitmapHelper.loadBitmap();
        @Nullable final List<NNF_FaceInfo> list = context.detectList(image);

        context.release();

        return null == list ? Collections.emptyList() : list;
    }

    protected void beforeInitializeDetectionContext(@NonNull final DetectionContext detectionContext) {
        detectionContext.getFunction().FACE_3DPOSE_ENABLED = true;
        detectionContext.getFunction().FACE_5LANDMARKS_ENABLED = true;
        detectionContext.getFunction().FACE_106LANDMARKS_ENABLED = true;
        detectionContext.getFunction().FACE_AGE_ENABLED = true;
        detectionContext.getFunction().FACE_GENDER_ENABLED = true;
    }

}
